package com.surfilter.modules.user.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.surfilter.modules.role.model.RoleModel;
import com.surfilter.modules.user.model.UserModel;
import com.surfilter.platform.base.dao.DaoBase;
import com.surfilter.platform.base.model.PageInfo;

@Repository
public class UserDao extends DaoBase<UserModel> {

	@Resource(name = "surfilterSqlMapClient")
	private SqlMapClient sqlMapClient;

	public int addUser(UserModel userModel) {
		int key = (Integer) this.addObject("addUser", userModel);
		return key;
	}

	public void chgUser(UserModel userModel) {
		this.updateObject("chgUser", userModel);
	}
	
	public void delUser(UserModel userModel) {
		this.updateObject("delUser", userModel);
	}

	public List<UserModel> getUsers(UserModel userModel, PageInfo pageInfo) {
		List<UserModel> retList = this.getListByObjectForPage("getUsers", userModel, pageInfo);
		for (UserModel user : retList) {
			int id = user.getCreateUser();
			user.setCreateUserName((String)this.getObjectByObject("getUserNameById", id));
			id = user.getUpdateUser();
			if(id != 0){
				user.setUpdateUserName((String)this.getObjectByObject("getUserNameById", id));
			}
		}
		return retList;
	}

	public UserModel getUserByName(UserModel userModel) {
		return (UserModel) this.getObjectByObject("getUserByName", userModel);
	}

	public UserModel getUserByLogin(UserModel userModel) {
		UserModel retModel = (UserModel) this.getObjectByObject(
				"getUserByLogin", userModel);
		return retModel;
	}
	
	public void delUsers(List<Integer> userIds){
		this.deleteObject("delUsers", userIds);
	}

	public void initSqlMapClient() {
		this.setSqlMapClient(sqlMapClient);
	}

	public int getUsersCount(UserModel userModel) {
		return (Integer) this.getObjectByObject("getUsersCount", userModel);
	}

	public List<UserModel> getUserByRoleId(int roleId) {
		return this.getListByObject("getUsersByRoleIds", new RoleModel(roleId));
	}

	public UserModel getUserByID(int id) {
		return (UserModel)this.getObjectByObject("getUserByID", id);
	}

	/**
	 * @param nowUser
	 */
	public void chgUserPwd(UserModel user) {
		this.updateObject("chgUserPwd", user);
	}
}
